package com.croqodile {
    import flash.events.*;
    import flash.utils.ByteArray;
    import flash.utils.Timer;
    import flash.net.Socket;
    import com.senocular.utils.Output;
    import com.croqodile.events.*;
    
    public class RouterMessageConnection extends RouterConnection {
	
	public static function create(userId:String, host:String, port:int):RouterMessageConnection{
	    var con:RouterMessageConnection = new RouterMessageConnection(userId, host, port);
	    con.connect();
	    return con;
	}
	
	
	public function RouterMessageConnection(userId:String, host:String, port:int) {
	    super(userId, host, port);
	}	
	
	///////////////////////////
        //    Event Handlers	 //
        ///////////////////////////
	
	override protected function onSentenceReceived(data:String):void {
	    if(this._loggedIn){
		this.dispatchEvent(new ExternalMessageEvent(data));
	    }
	    else if(data == LOGIN_ACK){
		Output.trace("Received LOGINOK.");
		this._loggedIn = true;
		this.dispatchEvent(new Event(CONNECTION_READY));
	    }
	}

	override protected function onSocketConnectError(event:IOErrorEvent):void {
	    throw new Error("An error occurred wile connecting to the router message server.");
	}
	
    }
}